use std::sync::OnceLock;

use global::init_wx;
use tauri_plugin_http::reqwest::Client;

mod global;
mod http;

static CLIENT: OnceLock<Client> = OnceLock::new();

pub fn get_client() -> &'static Client {
    CLIENT.get_or_init(|| Client::new())
}

#[tauri::command]
async fn bz_login(username: &str, password: &str) -> Result<global::BzUser, String> {
    match http::client::bz::login(username.to_string(), password.to_string()).await {
        Ok(user) => {
            Ok(user)
        }
        Err(str) => Err(str),
    }
}

// #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_log::Builder::new().build())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_shell::init())
        .invoke_handler(tauri::generate_handler![bz_login, init_wx])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
